import { GlobalFunction } from "../base/GlobalFunction";

const {ccclass, property} = cc._decorator;

@ccclass
export default class fllowFunc extends cc.Component {

    @property(cc.RigidBody)
    body:cc.RigidBody = null;
    @property(cc.Node)
    item:cc.Node = null;
    @property()
    speed:number = 1;
    @property()
    distance:number = 20;
    @property()
    accelerated:boolean = true; 
    
    setFllowItem(item:cc.Node){
        this.item = item;
    }

    update (dt) {
        if(!this.item) {
            return;
        }

        var dis = GlobalFunction.getTwoPointDis(this.item.position, this.node.position);
        if(dis >= this.distance){
            var dirVec = GlobalFunction.getTwoPointVec(this.item.position, this.node.position);
            if(this.accelerated)
                dirVec.scale(cc.v2(this.speed, this.speed), dirVec);
            else
                dirVec.normalize(dirVec).scale(cc.v2(this.speed, this.speed), dirVec);
            this.body.linearVelocity = dirVec;
        }
        else
        {
            this.body.linearVelocity = cc.v2(0,0);
        }
    }
}
